/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */ 
import store from '../vuex/store'; 
import Vue from 'vue'; 
const i = store.state.indexToInsert, s = store.state.charToInsert;
const timestampToTime = (timestamp, addTime) => {
  if (timestamp) {
    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    if (timestamp.length == 13) {
      date = new Date(parseInt(timestamp));
    }

    if (addTime) {
      let date_s = date.getTime();
      date.setTime(date_s + addTime * 1000);
    }
    let Y = date.getFullYear() + '-'
    let M =
      (date.getMonth() + 1 < 10 ?
        '0' + (date.getMonth() + 1) :
        date.getMonth() + 1) + '-'
    let D =
      (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
      (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
      (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
      ':'
    let s =
      date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s
  }

};

const timestampSubTime = (timestamp, addTime) => {
  if (timestamp) {
    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    if (timestamp.length == 13) {
      date = new Date(parseInt(timestamp));
    }

    if (addTime) {
      let date_s = date.getTime();
      date.setTime(date_s - addTime * 1000);
    }
    let Y = date.getFullYear() + '-'
    let M =
      (date.getMonth() + 1 < 10 ?
        '0' + (date.getMonth() + 1) :
        date.getMonth() + 1) + '-'
    let D =
      (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
      (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
      (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
      ':'
    let s =
      date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s
  }

};

const timestampToData= (timestamp,addTime) => {
  let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  if(addTime) {
    let date_s = date.getTime(); 
    date.setTime(date_s + addTime * 1000 * 60 * 60);
  }
  let Y = date.getFullYear() + '-'
  let M =
    (date.getMonth() + 1 < 10 ?
      '0' + (date.getMonth() + 1) :
      date.getMonth() + 1) + '-'
  let D =
    (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
  return Y + M + D
};
const phoneReg = /^(1[3-9])[0-9]{9}$|^(\+?84)?(0)?9[0-9]{9}$|^\d{7,15}$/;
const emailReg =  /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

const numberNo = (value,s,e) => { 
  let _len = value.length;
  return value.substring(0, s) + ' **** **** ' + value.substring(_len - e, _len)
}

const formatTime = (dateString) => {
  const date = new Date(dateString);
  const options = { month: '2-digit', day: '2-digit' };
  return date.toLocaleDateString(undefined, options); 
}
const  formatDate = (dateString)=>  {
  const date = new Date(dateString);
  const options = { month: '2-digit', day: '2-digit', year: 'numeric' };
  return date.toLocaleDateString(undefined, options); // 使用浏览器的默认语言设置
}

const spStr = (str,sp,rp) => {
  var regex = new RegExp(sp, 'g');
  return str.replace(regex, rp);

}
 
/**
 * 存储localStorage
 */
const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}


/**
 * 存储sessionStorage
 */
 const setStoreS = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.sessionStorage.setItem(name, content);
}

/**
 * 获取sessionStorage
 */
const getStoreS = name => {
  if (!name) return;
  return window.sessionStorage.getItem(name);
}

/**
 * 删除sessionStorage
 */
const removeStoreS = name => {
  if (!name) return;
  window.sessionStorage.removeItem(name);
}

/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
  let date = new Date();
  date.setDate(date.getDate() + day);
  document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
function getCookie(name) {
  let reg = RegExp(name + '=([^;]+)');
  let arr = document.cookie.match(reg);
  if (arr) {
    return arr[1];
  } else {
    return '';
  }
};

/**
 * 删除cookie
 **/
function delCookie(name) {
  setCookie(name, null, -1);
};


function insertCharacter(str) {
  return str.slice(0, i) + s + str.slice(i);
};

function deleteCharacter(str) {
  return i >= 0 && i < str.length
    ? str.slice(0, i) + str.slice(s.length + i)
    : str;
};

 

/**
 * 导出 
 **/
export {
  timestampToTime,
  timestampSubTime, 
  setStore,
  getStore,
  removeStore,
  setStoreS,
  getStoreS,
  removeStoreS,
  setCookie,
  getCookie,
  delCookie,
  insertCharacter,
  deleteCharacter,
  timestampToData,
  numberNo,
  formatTime,
  formatDate, 
  phoneReg,
  emailReg,
  spStr, 
}